package com.FTW.SAA.config;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import com.alibaba.cloud.ai.memory.redis.RedisChatMemoryRepository;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class SAA_Config {

    private final String DeepSeekModel = "deepseek-v3.1";
    private final String Qwen3Model = "qwen3-max-2025-09-23";


    @Bean(name = "deepseek")
    public ChatModel deepSeekModel() {
        return DashScopeChatModel.builder()
                .dashScopeApi(
                        DashScopeApi.builder().apiKey(System.getenv("AI_DASHSCOPE_API_KEY")).build())
                .defaultOptions(
                        DashScopeChatOptions.builder().withModel(DeepSeekModel).build()
                )
                .build();
    }

    @Bean(name = "qwen3")
    public ChatModel qwen3Model() {
        return DashScopeChatModel.builder()
                .dashScopeApi(
                        DashScopeApi.builder().apiKey(System.getenv("AI_DASHSCOPE_API_KEY")).build())
                .defaultOptions(
                        DashScopeChatOptions.builder().withModel(Qwen3Model).build()
                )
                .build();
    }

    @Bean(name = "deepSeekClient")
    public ChatClient deepSeekChatClient(@Qualifier("deepseek") ChatModel chatModel) {
        return ChatClient.builder(chatModel).build();
    }

    @Bean(name = "qwen3Client")
    public ChatClient qwen3ChatClient(@Qualifier("qwen3") ChatModel chatModel,
            RedisChatMemoryRepository redisChatMemoryRepository) {
        MessageWindowChatMemory messageWindowChatMemory = MessageWindowChatMemory.builder()
                .chatMemoryRepository(redisChatMemoryRepository)
                .maxMessages(10)
                .build();

        return ChatClient.builder(chatModel)
                .defaultOptions(ChatOptions.builder().model(Qwen3Model).build())
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(messageWindowChatMemory).build())
                .build();
    }

    @Bean(name = "ollamaClient")
    public ChatClient ollamaChatClient(ChatModel ollamaChatModel) {
        return ChatClient.builder(ollamaChatModel).build();
    }


}
